package com.UnionPayCenter.util;

import org.apache.commons.lang3.StringUtils;

public class Asserts {

    public static void notBlank(String str, String args) {
        if (StringUtils.isBlank(str)) {
            throw new IllegalArgumentException(String.format("[%s]不能为空", args));
        }
    }

    /**
     * one of them is not blank
     *
     * @param args
     * @param strs
     */
    public static void leastOneNotBlank(String args, String... strs) {
        for (String str : strs) {
            if (StringUtils.isNotBlank(str)) {
                return;
            }
        }
        throw new IllegalArgumentException(String.format("[%s]不能为空", args));
    }

    public static <T> void notNull(T t, String args) {
        if (t == null) {
            throw new IllegalArgumentException(String.format("[%s]不能为空", args));
        }
    }

    public static void naturalNumber(String val, String arg) {
        if (StringUtils.isBlank(val)) {
            throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
        }
        try {
            Asserts.naturalNumber(Integer.parseInt(val), arg);
        } catch (NumberFormatException e) {
            try {
                Asserts.naturalNumber(Long.parseLong(val), arg);
            } catch (NumberFormatException e1) {
                throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
            }
        }
    }

    public static void naturalNumber(Long val, String arg) {
        if (val == null || val < 0) {
            throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
        }
    }

    public static void naturalNumber(Integer val, String arg) {
        if (val == null || val < 0) {
            throw new IllegalArgumentException(String.format("%s不是有效自然数", arg));
        }
    }
}
